package cn.icatw.leetcode.editor.cn;

import java.util.Scanner;

/**
 * @author 王顺 762188827@qq.com
 * @apiNote
 * @since 2024/3/28
 */
public class YunZhiTest3 {
    public static void main(String[] args) {
        //    给定一个字符串，请你求出有多少个连续子串包含'r'和'e'字符，但不包含'd'字符？
        Scanner in = new Scanner(System.in);
        String a = in.nextLine();
        System.out.println(countSubStringsWithoutD(a));
    }

    private static int countSubStringsWithoutD(String s) {
        int count = 0, left = 0, right = 0, rCount = 0, eCount = 0;
        while (right < s.length()) {
            char currentChar = s.charAt(right);
            if (currentChar == 'r') {
                rCount++;
            } else if (currentChar == 'e') {
                eCount++;
            } else if (currentChar == 'd') {
                while (left <= right && s.charAt(left) != 'r' && s.charAt(left) != 'e') {
                    if (s.charAt(left) == 'r') {
                        rCount--;
                    } else if (s.charAt(left) == 'e') {
                        eCount--;
                    }
                    left++;
                }
            }
            if (rCount > 0 && eCount > 0) {
                count++;
            }
            right++;
        }
        return count;
    }

    private static int countSubStrings(String s) {
        int count = 0;
        int left = 0, right = 0;
        while (right < s.length()) {
            //    当右边界字符满足条件时，移动右边界
            if (s.charAt(right) == 'r' || s.charAt(right) == 'e') {
                right++;
            } else if (s.charAt(right) == 'd') {
                while (left <= right && s.charAt(left) != 'r' && s.charAt(left) != 'e') {
                    left++;
                }
                left++;
            } else {
                right++;
            }
            //    检查当前子串是否同时包括'r'和'e'字符，若包含，则计数器加1
            if (isContained(s, left, right)) {
                count++;
            }
        }
        return count;
    }

    private static boolean isContained(String s, int left, int right) {
        boolean hasR = false, hasE = false;
        for (int i = left; i < right; i++) {
            if (s.charAt(i) == 'r') {
                hasR = true;
            } else if (s.charAt(i) == 'e') {
                hasE = true;
            }
            //    d
            if (s.charAt(i) == 'd') {
                return false;
            }
            if (hasR && hasE) {
                return true;
            }
        }
        return hasR && hasE;
    }
}
